package com.tjn.exercise;

public class Person {
    private String name;
    private Vehicles vehicles;

    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }

    //可以把具体的要求封装到方法里面
    //下面如果直接这样写，那么对象传进去一个Horse，调用下面两个方法之后
    // 不光Horse能用，Boat也能用，这样就感觉传进去的Horse没用，所以需要优化
    public void passRiver(){
        //判断当前的vehicles属性为null，就获取一艘船
        // 现在已经可以用得上传进去的Horse了，但是这样写在该用船的时候也用的是马，所以还要改
        //if(vehicles == null){
        if(!(vehicles instanceof Boat)){
            //vehicles = null:vehicles instanceof Boat->false->整体true
            //vehicles = Horse:vehicles instanceof Boat->false->整体true
            //vehicles = Boat:vehicles instanceof Boat->true->整体false
            vehicles = Factory.getBoat();
        }
        vehicles.work();
//        Boat boat = Factory.getBoat();
//        boat.work();
    }
    public void common(){
        //判断当前的vehicles属性为null，就获取一匹马
        //if(vehicles == null){
        if(!(vehicles instanceof Horse)){
            vehicles = Factory.getHorse();
        }
        vehicles.work();
//        Horse horse = Factory.getHorse();
//        horse.work();
    }
}
